<?php

namespace App\Models;

use App\User;
use Illuminate\Database\Eloquent\Model;

class MemberCoupon extends Model
{
    protected $table = 'member_coupons';

    protected $fillable = [
        'coupon_id', 'user_id', 'use_at', 'start_time', 'end_time'
    ];

    protected $hidden = [
        'created_at', 'updated_at'
    ];
    public function user()
    {
        return $this->hasOne(User::class,'id', 'user_id');
    }

    public function coupon()
    {
        return $this->hasOne(Coupon::class,'id', 'coupon_id');
    }

    // 未过期
    public function scopeBegin($query)
    {
        return $query->where('start_time', '<', now())->where('end_time', '>', now());
    }

    // 未使用
    public function scopeUse($query)
    {
        return $query->where('use_at', null);
    }
}
